package iosr11.gbase.dao;

import iosr11.data.ProjectGroup;
import iosr11.data.ProjectSubject;

import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

public class ProjectSubjectDAOImpl extends GenericDAOImpl<ProjectSubject> implements ProjectSubjectDAO {

	private static String TYPE = "project subject";
	private static String TOPIC = "topic";
	private static String DESCRIPTION = "subject description";
	private static String GROUP_REF = "group";
	
	private Logger log = Logger.getLogger("service-log");
	
	
	@Override
	public ProjectSubject getByTopic(String topic) {
		// TODO Auto-generated method stub
		
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query, TOPIC, topic);
		if (feed==null){
			log.info("brak rekordow w bazie");
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed==null){
			log.info("brak rekordow w bazie o odpowiednim typie");
			return null;
		}
		if (feed.getEntries().size()!=1){
			log.info("zla ilosc rekordow w bazie");
			return null;
		}
		
		return this.rewrite(feed.getEntries().get(0));
	}

	@Override
	public ProjectSubject getSubjectForGroup(ProjectGroup group) {
		if (group==null){
			log.warning("brak grupy - null");
			return null;
		}
		// TODO Auto-generated method stub
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query, GROUP_REF, group.getId());
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		
		if (feed == null) {
			log.warning("brak tematu dla grupy");
			return null;
		}
		if (feed.getEntries().size()!=1) {
			log.warning("zla ilosc rekordow (teamtow) odnoszacych sie do jednej grupy (rozna od 1)");
			return null;
		}
		
		return this.rewrite(feed.getEntries().get(0));
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	@Override
	public ProjectSubject rewrite(GoogleBaseEntry entry) {
		ProjectSubject entity = new ProjectSubject();
		entity.setId(entry.getId());
		entity.setTopic(entry.getGoogleBaseAttributes().getTextAttribute(TOPIC));
		entity.setDescription(entry.getGoogleBaseAttributes().getTextAttribute(DESCRIPTION));
		if (!entry.getGoogleBaseAttributes().getTextAttribute(GROUP_REF).contains("http")){
			if (!entry.getGoogleBaseAttributes().getTextAttribute(GROUP_REF).contains("null")){
				log.info("zly rekord -omijanie -null");
			}
			entity.setGroup(null);
			log.info("brak przypisanej grupy do tematu");
		}
		else {
			log.info("przypisywanie grupy do tematu");
			ProjectGroup group = new ProjectGroup();
			group.setId(entry.getGoogleBaseAttributes().getTextAttribute(GROUP_REF));
			entity.setGroup(group);
		}
		return entity;
	}

	@Override
	public GoogleBaseEntry rewrite(ProjectSubject entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.setDraft(true);
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		entry.getGoogleBaseAttributes().addTextAttribute(TOPIC, entity.getTopic());
		entry.getGoogleBaseAttributes().addTextAttribute(DESCRIPTION, entity.getDescription());
		if (entity.getGroup()!=null){
			if (entity.getGroup().getId()!=null){
				entry.getGoogleBaseAttributes().addTextAttribute(GROUP_REF, entity.getGroup().getId());
				return entry;
			}
		}
		log.info("ustawienie referencji na grupe na null");
		entry.getGoogleBaseAttributes().addTextAttribute(GROUP_REF, "null");
		return entry;
	}
	
}
